package main

import (
	"context"
	"fmt"
	"gitee.com/xkgo/tlog"
	"go.uber.org/zap"
	"io"
	"sync"
	"time"
)

func main() {
	err := tlog.InitLogCommon(
		tlog.LogTimestampWithMs(true),
		tlog.FunctionKey("func"))
	if err != nil {
		panic(err)
	}
	// 异步日志必须要做 defer sync，否则可能丢失最后几条日志
	defer tlog.Sync()

	ctx := context.WithValue(context.Background(), "traceID", fmt.Sprint(time.Now().UnixNano()))

	tlog.Info(ctx, "a", zap.String("strVal", "foo"))

	tlog.Warn(ctx, "b", zap.Int("intVal", 1234))

	tlog.Error(ctx, "c", zap.Duration("durVal", time.Millisecond*567))

	tlog.GetStdLogger().Println("error:", 404, "not found")

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		defer func() {
			if r := recover(); r != nil {
				tlog.Info(ctx, "e", zap.Any("anyVal", r))
			}
		}()
		tlog.Panic(ctx, "d", zap.Any("anyVal", struct {
			K1 string
			K2 int
		}{K1: "bar", K2: 890}))
	}()
	wg.Wait()

	tlog.Fatal(ctx, "f", zap.Error(io.EOF))
}
